package com.xxx.springboot.controller;

import cn.shuibo.annotation.Decrypt;
import com.xxx.springboot.pojo.bean.Product;
import com.xxx.springboot.service.IndexService;
import com.xxx.springboot.service.ProductService;
import io.redisearch.SearchResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.GeoCoordinate;

/**
 * RediSearch Controller
 */
@RestController
@RequestMapping(value = "/", produces = "application/json;charset=utf-8")
public class RediSearchController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(RediSearchController.class);

	@Autowired
	private IndexService indexService;
	@Autowired
	private ProductService productService;

	@GetMapping("/createProductIndex")
	public String createProductIndex(){
		// 1. 创建索引
		indexService.createProductIndex();

		// 2. 添加数据
		Product p1 = new Product("1", "无线蓝牙耳机", 299.0, "电子产品", new GeoCoordinate(116.397469, 39.908821));
		productService.addProduct(p1);

		// 3. 执行高级搜索
		SearchResult result = productService.searchProducts("@category:{电子产品} @price:[200 300] @location:[116.397469 39.908821 10 km]");

		// 4. 输出高亮结果
		result.docs.forEach(doc -> System.out.println("高亮标题：" + doc.get("title")));

		return "success";
	}

}
